<?php
class StoreModule
{
	function index() {
		global $_G;
		$id = getGP('id','G','int');
		$action = getGP('a','G');
		if($id){
			$history=getGP('history','c');
			if($history)$history=explode(',',$history);
			if(empty($history))$history=array();
			if(!in_array($id,$history)){
			if(count($history)>9 )array_pop($history);
			array_unshift($history, $id);
			set_cookie('history',implode(',', $history),100000000);
			}
			$store=$_G['db']->fetch('shops','*',array('shopid'=>$id));
			$query = $_G['db']->query("SELECT cid,catename,total FROM".table('category')."WHERE uid='".$store['uid']."' AND type=2 ");
			while ($data=$_G['db']->fetch_array($query)){
				$shopcates[$data['cid']]=$data;
			}
			//$shopcates = $_G['db']->fetch_all("SELECT cid,catename,total FROM".table('category')."WHERE uid='".$store['uid']."' AND type=2 " );
			switch($action){
				case 'review':
					$query = $_G['db']->query("SELECT * FROM".table('comment')."WHERE shopid='".$id."' AND type=0  ORDER BY cid DESC " );
					while($data=$_G['db']->fetch_array($query)){
						$results[]=$data;
					}
					foreach($comments as $k => $v){
		        		if($v['pid'] > 0){
		        			$comments[$v['pid']]['subcomment'][$v['cid']] = $v;
		        			$comments[$k] = null;
		        			unset($comments[$k]);
		        		}
	        		}	
					break;
				case 'feedback':
					$query = $_G['db']->query("SELECT * FROM".table('comment')."WHERE shopid='".$id."' AND type=1 AND status=1 AND ishide=0 ORDER BY cid DESC " );
					while($data=$_G['db']->fetch_array($query)){
						$comments[]=$data;
					}
					foreach($comments as $k => $v){
		        		if($v['pid'] > 0){
		        			$comments[$v['pid']]['subcomment'][$v['cid']] = $v;
		        			$comments[$k] = null;
		        			unset($comments[$k]);
		        		}
	        		}	
					break;
				case 'offer':
					break;
				case 'map':
					break;			
				default:
					//菜单
					//$goods = $_G['db']->fetch_all("SELECT * FROM".table('caidan')."WHERE shopid='".$id."'  GROUP BY cid ORDER BY recommend DESC,pid DESC " );
					$query = $_G['db']->query("SELECT * FROM".table('product')."WHERE shopid='".$id."'  ORDER BY recommend DESC,pid DESC " );
					while($data=$_G['db']->fetch_array($query)){
						$goods[$data['cid']][]=$data;
					}	
			}
			
			//购物车
			m('cart');
			$carts = CartModule::get_user_cart($id);
//			foreach($cart as $k=>$v){
//				$carts[$v['shopid']][$k] = $v;
//			}
			$_G['title'] = $store['shopname'];
		    $cates = $_G['cache']->get('category');
			loadialog();
		}
		include template('store');
	}

	function getshopbyid($shopid){
		global $_G;
		return $_G['db']->fetch("shops","*",array('shopid'=>intval($shopid)));
	}
	function getshopbyuid($uid){
		global $_G;
		return $_G['db']->fetch("shops","*",array('shopid'=>intval($uid)));
	}
	
	//餐厅列表页
	function shop_list(){
		global $_G;
		$destid = getGP('dest','G','int');
		$regionid = $_REQUEST['region'];
		$qprice = $_REQUEST['qprice'];
		$cprice = $_REQUEST['cprice'];
		$type = getGP('type','G','int');
		$keyword = getGP('key','G');
		$limit=10;
		if($destid){
			$where .= " AND dest='".$destid."'";
			$url .= '&dest='.$destid;
		}
		if($type){
			$where .= " AND cid='".$type."'";
		}
		
		if($regionid){
			$where .= " AND region='".$regionid."'";
			$url .= '&region='.$regionid;
		}
		switch($qprice){
			case 0:
				break;
			case 1:
				$where .= " AND minprice <10 ";
				break;
			case 2:
				$where .= " AND minprice BETWEEN 10 AND 29 ";
				break;
			case 3:
				$where .= " AND minprice BETWEEN 30 AND 49 ";
				break;
			case 4:
				$where .= " AND minprice BETWEEN 50 AND 100 ";
				break;
			case 5:
				$where .= " AND minprice >100 ";
				break;	
		}
		switch($cprice){
			case 0:
				break;
			case 1:
				$where .= " AND extprice <1 ";
				break;
			case 2:
				$where .= " AND extprice BETWEEN 1 AND 2 ";
				break;	
			case 3:
				$where .= " AND extprice BETWEEN 3 AND 5 ";
				break;	
			case 4:
				$where .= " AND extprice BETWEEN 6 AND 10 ";
				break;	
			case 5:
				$where .= " AND extprice >10 ";
				break;	
		}
		if($regionid && !$destid){
			foreach($region as $kid => $v){
				foreach($v as $rid => $cv){
					if($rid == $regionid) {
						$destid = $kid;break;
					}
				}
				if($destid)break;
			}
		}
		$now = gdate('H:i',TIMESTAMP);
		$now = gtime($now);
		
		$worktime1 = array('05:00','05:30','06:00','06:30','07:00','07:30','08:00','08:30','09:00','09:30','10:00','10:30','11:00','11:30');
		$worktime2 = array('12:00','12:30','13:00','13:30','14:00','14:30','15:00','15:30','16:00','16:30','17:00','17:30','18:00','18:30','19:00','19:30','20:00','20:30','21:00','21:30','22:00','22:30','23:00','23:30');
		
		$query = $_G['db']->page_query("SELECT * FROM".table('shops')."WHERE 1".$where,$limit);
		//$query = $_G['db']->query("SELECT * FROM".table('shops')."WHERE 1".$where);
		$num = $_G['db']->result("SELECT count(*) FROM".table('shops')."WHERE 1".$where);
		while($data=$_G['db']->fetch_array($query)){
			$results[$data['shopid']]=$data;
			$stats[$data['cid']]+=1;
			if($now<gtime($worktime1[$data['opentime']]) || $now > gtime($worktime2[$data['closetime']])){
				$results[$data['shopid']]['shopclose'] = true;
			}
		}
		//vd($results);
		if($destid){
			$_G['title'] = $dest[$destid].'附近的外卖';
		}
		if($regionid){
			$_G['title'] = $region[$destid][$regionid].'附近的外卖';
		}
		$history=getGP('history','c');
		if($history){
			$historys=$_G['db']->fetch_all('SELECT shopid,shopname FROM'.table('shops').'WHERE shopid IN('.$history.')');
		}
		$pager = pager($num,$limit);
		$cates = $_G['cache']->get('category');
		include template('search');
	}

}	